﻿using AspNet.Mvc.Grid.Pagination;
using FangDuShi_Admin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FangDuShi_Admin.Controllers
{
      [OutputCache(Duration = 0, VaryByParam = "none")]
    public class HSVersionPCController : AdminBaseController
    {
        int Pagesize = 10;
        BLL.HSVersionPC verBll = new BLL.HSVersionPC();
        //
        // GET: /HSVersionPC/
        public ActionResult Index(string Page)
        {
            int pageout;
            if (!int.TryParse(Page, out pageout))
            {
                pageout = 1;
            }
            List<Model.HSVersionPC> verList = verBll.GetModelList("");
            IPagination<Model.HSVersionPC> PverList = verList.AsPagination(pageout, Pagesize);
            PagerData pagerInfo = new PagerData(pageout, PverList.TotalItems, Pagesize);
            ViewData["pageout"] = pageout;
            ViewData["Page"] = pagerInfo;
            return View(PverList);
        }
        public ActionResult VerAdd()
        {

            return VerEdit("VerEdit", "0", BaseStruct.ActionOperationStruct.ADD_NEXT);

        }

         public ActionResult VerEdit(string FID)
        {
            return VerEdit("VerEdit", FID, BaseStruct.ActionOperationStruct.EDIT);
        }
        private ActionResult VerEdit(string viewname, string FID, string actionOP)
        {
            Model.HSVersionPC mdl = new Model.HSVersionPC();

            switch (actionOP)
            {
                case BaseStruct.ActionOperationStruct.ADD_NEXT:
                    {
                        mdl.FIsMustUpdate = "N";
                        break;
                    }
                case BaseStruct.ActionOperationStruct.EDIT:
                    {

                        mdl = verBll.GetModel(decimal.Parse(FID));
                        break;
                    }
            }

            ViewData["hdMethod"] = actionOP.ToString();
            return View(viewname, mdl);
        
        }

        public string DelList(string ids)
        {

            List<string> listid = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            string returnstr = verBll.DelVerList(listid);
            if (returnstr == "ok")
            {
                return "ok";
            }
            else
            {
                return "删除失败错误原因:" + returnstr;
            }
        }
        public string Save()
        {

            Model.HSVersionPC mdl = new Model.HSVersionPC();

            string FVersionNo = Fetch.Post("FVersionNo").Trim();
            decimal FVersionNoout;
            if (!decimal.TryParse(FVersionNo,out FVersionNoout))
            {
                return "错误:版本号必须为数字";
            }
            List<Model.HSVersionPC> listVer = verBll.GetModelList("FVersionNo ='" + FVersionNo + "'");
            if (listVer.Count>0)
            {
                return "错误:已存在相同的版本号";
            }
            string FIsMustUpdate = Fetch.Post("FIsMustUpdate").Trim().ToLower()=="on"?"Y":"N";
            string note = Fetch.Post("fck1").Trim();
            mdl.FPubDate = DateTime.Now;
            mdl.FContent = note;
            mdl.FVersionNo = FVersionNoout.ToString();
            mdl.FIsMustUpdate = FIsMustUpdate;
            switch (Fetch.Post("hdMethod"))
            {
                case BaseStruct.ActionOperationStruct.ADD_NEXT:
                    {

                        try
                        {
                            decimal outid = verBll.Add(mdl);

                            if (outid != 0)
                            {
                                return "操作成功";
                            }
                        }
                        catch (System.Exception ex)
                        {
                            return "错误:" + ex.Message;
                        }
                        break;
                    }
                case BaseStruct.ActionOperationStruct.EDIT:
                    {

                        try
                        {
                            string FID = Fetch.Post("hFID");
                            mdl.FID = decimal.Parse(FID.ToString());
                            if (verBll.Update(mdl))
                            {
                                return "操作成功";
                            }
                            else
                            {

                                return "错误:保存数据失败";
                            }
                        }
                        catch (System.Exception ex)
                        {
                            return "错误:" + ex.Message;

                        }
                        break;
                    }
            }

            return "错误:保存数据失败";
        }
    }
}